/*******************************************************************************
Wrapper.do

This do-file runs all the necessary Stata code for the empirical section of
"Plants in Space".

Last updated: 02/28/2023
*******************************************************************************/

cd "C:\Plants_in_Space"

version 15
set more off
set type double
set varabbrev off
graph set window fontface "Times New Roman"

// Set up project directories
do "Code\set_directories.do"

// List SSC commands that need to be installed
ssc install spgrid 		// To divide U.S. into grid
ssc install shp2dta	    // Converting shapefiles to Stata .dta files
ssc install mergepoly 	// To merge state polygons into one polygon for the US
ssc install geo2xy 		// Converting from lat/lon coordinates to projections
ssc install geoinpoly	// To map latitude-longitude coordinates into grid IDs
ssc install outreg2		// To export regression results to LaTeX
ssc install reghdfe 	// For faster fixed effects regressions
ssc install binscatter  // To do binned scatterplots
grmap, activate			// To create maps from shapefiles

***********************************DATA SETUP***********************************
// Divides the continental US into M-by-M mile squares (M=3, 6, 12, 24, 48).
do "Code\Setup\Continental_US_Grids.do"

// Calculates population and employment by census block, and maps this data into 
// M-by-M mile squares.
do "Code\Setup\Grid_Population_and_Employment_Data.do"

// Queries and cleans the 2014 NETS data, and maps NETS establishments into
// M-by-M mile squares.
do "Code\Setup\Query_and_Clean_NETS_Data.do"

// Calculates population density and rent per square foot by ZIP code and by
// county in the US.
do "Code\Setup\Rent_and_Population_Data.do"

// Processes NETS data for Figure 13 and Tables I, II, and V. For each firm,
// computes average employment density across all locations in which the firm is
// located.
do "Code\Setup\NETS_Sorting_Data.do"

// Processes NETS data for Tables III and VI. For each industry-location, finds
// population density and the national size of the firm with the most plants. 
do "Code\Setup\NETS_Largest_Firm_in_Town_Data.do"

// Processes NETS data for Tables IV, VII, and VIII. For each firm-location, 
// computes total firm employment excluding the own firm contribution of 
// employment in the location, average employment, and total number of plants.
do "Code\Setup\NETS_Span_of_Control_Data.do"


// Produces the data for Regression Table X, the case of delta = 1.
shell "Code\Setup\regressionMatrix_Ls"

// Produces the data for Regression Table X, the case of delta = 9.
shell "Code\Setup\regressionMatrixDelta9_Ls"

// Produces the data required to run the code "regressionMatrix_Ls" and also 
// produces the number of plants per firm when delta = 1.
shell "Code\Setup\wrapper_square"

// Produces the data required to run the code "regressionMatrixDelta9_Ls" and also 
// produces the number of plants per firm when delta = 9.
shell "Code\Setup\wrapper_square_delta9"


************************************ANALYSIS************************************
// Plots the Starbucks locations in the 12x12 mile squares that roughly contain 
// Princeton, Richmond, and New York City.
do "Code\Analysis\Figure1.do"

// Creates histograms showing the cumulative number of Walgreens and Rite Aid
// establishments by population density.
do "Code\Analysis\Figure2.do"


// This Matlab file creates two plots of the catchment areas where 9 plants are
// located in the squared space. In one case bO is constant and in the other bO varies.  
shell "Code\Analysis\Figure4"

//This Matlab code produces 6 plots that present the comparative statics with respect to
// sigma, and the baseline plots. 
shell "Code\Analysis\Figures8_9_10"

//This Matlab code produces 3 plots presenting the comparative statics with respect to phi
shell "Code\Analysis\Figures11_12"

// Creates a plot showing a kernel regression of the relationship between log 
// average firm employment density and log total firm employment, after 
// controlling for industry fixed effects.
do "Code\Analysis\Figure13.do"

// Creates tables showing sorting results by regressing log average employment 
// density across all locations in which a firm is located on the firm's log
// total employment and industry fixed effects.
do "Code\Analysis\TablesI_II_V.do"

// Creates tables to show largest firm in town results by regressing log total 
// employment of the firm with the most plants in an industry-location pair on 
// log population density and industry fixed effects, weighted by industry 
// employment.
do "Code\Analysis\TablesIII_VI.do"

// Creates tables to show span of control results by regressing log 
// firm-location average employment on log total firm employment excluding the 
// own firm contribution of employment in the location, log firm-location number
// of plants and its square, and industry-location fixed effects.
do "Code\Analysis\TablesIV_VII_VIII.do"


// Produces the first row of table IX in the draft which shows the number
// of plants per firm when delta = 1. 
shell "Code\Analysis\TableIX_a"


// Produces the first row of table IX in the draft which shows the number
// of plants per firm when delta = 9. 
shell "Code\Analysis\TableIX_b"

// Produces the regression table X that shows the results of regressing firm's 
// employment on its local weighted population. 
do "Code\Analysis\TableX.do"


// Creates binscatters of rents per square foot versus population density at the 
// ZIP code and county levels.
do "Code\Analysis\FigureB1.do"

// Creates two graphs of figureG1 presenting normalized tightest bound and 
// the number of establishments.  
shell "Code\Analysis\FigureG1"


